package com.example.student01.controller;

import com.example.student01.model.Student;
import com.example.student01.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class StuController {

    @Autowired
    StuService stuService;

    @GetMapping("/students")
//    @CrossOrigin(origins = "http://localhost:8080")
    public List<Student> getAllStu(){
        return stuService.list();
    }

    @GetMapping("/student/{id}")
    public Student getStuById(@PathVariable("id") Integer id){
        return stuService.getById(id);
    }

    @PostMapping("/student")
    public String addStu(@RequestBody Student student){
        if (stuService.save(student)){
            return "添加用户成功";
        }else {
            return "添加用户失败";
        }
    }

    @PutMapping("/student")
    public String updateStu(@RequestBody Student student){
        if (stuService.updateById(student)){
            return "修改用户成功";
        }else {
            return "修改用户失败";
        }
    }

    @DeleteMapping("/student")
    public String deleteStu(Integer id){
        if (stuService.removeById(id)){
            return "删除用户成功";
        }else {
            return "删除用户失败";
        }
    }

}
